<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleCancel" center width="50%">
      <el-form ref="editFormRef" :model="editForm" :rules="rules" label-width="100px" size="small">
        <el-form-item label="部门名称" prop="deptName">
          <el-input placeholder="请输入部门名称" v-model="editForm.deptName"></el-input>
        </el-form-item>

        <el-form-item label="部门级别" prop="deptLevel">
          <el-select placeholder="请选择部门级别" v-model="editForm.deptLevel" @change="deptLevelChange">
            <el-option v-for="item in deptLevel" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="部门主管" prop="deptHead">
          <el-input placeholder="请输入部门主管" v-model="editForm.deptHead"></el-input>
        </el-form-item>

        <el-form-item label="主管电话" prop="headMobile">
          <el-input placeholder="请输入主管电话" v-model="editForm.headMobile"></el-input>
        </el-form-item>

        <el-form-item label="上级部门" prop="pdeptName" :show-message="deptLevelValue === 1 ? false : true">
          <el-input
            ref="deptInput"
            :disabled="disabled"
            placeholder="请选择上级部门"
            @focus="deptFocus"
            v-model="editForm.pdeptName"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否启用" prop="isEnable">
          <el-radio-group v-model="editForm.isEnable">
            <el-radio :label="1">账号可用</el-radio>
            <el-radio :label="0">不可用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            placeholder="请输入备注"
            type="textarea"
            v-model="editForm.remark"
            :maxlength="100"
            :autosize="{ minRows: 4, maxRows: 6 }"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel" size="small">取 消</el-button>
        <el-button type="primary" @click="handleOk('editFormRef')" size="small">确 定</el-button>
      </div>
    </el-dialog>
    <dept-tree-modal
      ref="deptModal"
      :checkDeptInfo="checkDeptInfo"
      :isOnlyShowParent="true"
      :deptLevel="deptLevelValue"
      @deptModalOk="deptModalOk"
    ></dept-tree-modal>
  </div>
</template>

<script>
import { deptLevel } from '../js/constants'
import DeptTreeModal from './DeptTreeModal'
import { addDepartment, modifyDepartment } from '../js/auth'
import pick from 'lodash.pick'

export default {
  name: 'DepartmentModal',
  components: { DeptTreeModal },
  data () {
    return {
      title: '部门新增',
      dialogVisible: false, // 编辑弹窗
      disabled: true,
      editForm: {
        deptName: '',
        deptLevel: '',
        deptHead: '',
        headMobile: '',
        pdeptName: '',
        isEnable: '',
        remark: ''
      },
      model: {},
      rules: {
        deptName: [{ required: true, message: '请输入部门名称!' }],
        deptLevel: [{ required: true, message: '请选择部门级别!' }],
        deptHead: [{ required: true, message: '请输入部门主管!' }],
        headMobile: [{ required: true, message: '请输入主管电话!' }],
        pdeptName: [{ required: true, message: '请选择上级部门!' }],
        isEnable: [{ required: true, message: '请选择是否启用!' }]
      },
      deptLevel,
      checkDeptInfo: {},
      deptLevelValue: 0
    }
  },
  watch: {
    // 校验规则替换
    deptLevelValue (val) {
      if (val === 1) {
        this.rules.pdeptName[0].required = false
        this.disabled = true
      } else {
        this.rules.pdeptName[0].required = true
        this.disabled = false
      }
    }
  },
  methods: {
    // 新增
    add () {
      this.edit({})
    },
    // 编辑
    edit (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      if (record.deptId) {
        this.title = '编辑部门'
        const { parentId: deptId, pdeptName: deptName, deptLevel } = record
        this.checkDeptInfo = {
          deptId,
          deptName
        }
        this.deptLevelValue = deptLevel
        this.$nextTick(() => {
          this.editForm = pick(
            this.model,
            'deptName',
            'deptLevel',
            'deptHead',
            'headMobile',
            'pdeptName',
            'isEnable',
            'remark'
          )
        })
      } else {
        this.title = '添加部门'
      }
    },

    // 提交表单
    handleOk (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          const values = {
            ...this.editForm,
            parentId: this.checkDeptInfo.deptId
          }
          let res, msg
          if (!this.model.deptId) {
            res = await addDepartment(values)
            msg = '保存成功！'
          } else {
            res = await modifyDepartment(Object.assign({}, values, { deptId: this.model.deptId }))
            msg = '修改成功！'
          }
          if (res.code === 0) {
            this.$message.success(msg)
          } else {
            this.$message.warning(res.msg)
          }
          this.$emit('ok')
          this.handleCancel()
        }
      })
    },

    // 上级部门弹框回调
    deptModalOk (val) {
      this.checkDeptInfo = val
      this.editForm.pdeptName = val.deptName
    },

    // 部门级别改变
    deptLevelChange (val) {
      this.deptLevelValue = val
      this.editForm.pdeptName = ''
      if (val === 1) {
        this.checkDeptInfo['deptId'] = 0
      }
    },

    // 获取焦点弹框
    deptFocus () {
      this.$refs.deptModal.add()
      this.$refs.deptInput.blur()
    },

    // 取消
    handleCancel () {
      this.dialogVisible = false
      this.$refs.editFormRef.resetFields()
      this.checkDeptInfo = {}
    }
  }
}
</script>

<style lang="scss" scoped></style>
